package com.example.duomeitijuhe.service;

import android.os.AsyncTask;
import com.example.duomeitijuhe.bean.GetInternetDataBean;
import com.example.duomeitijuhe.bean.UserToken;
import com.example.duomeitijuhe.constant.WeiboConstant;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by lx on 2014/10/16.
 */
public class WeiboGetNewWeibo extends AsyncTask<UserToken , Void , String> {

    private boolean  isOver ;

    private httpSupport httpSupport ;

    private GetInternetDataBean getInternetDataBean;

    private UserToken userToken ;

    public WeiboGetNewWeibo(){
        isOver = false ;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);



        try {
            JSONObject jsonObject = new JSONObject(s) ;
            JSONArray jsonArray = jsonObject.getJSONArray("statuses") ;

            for( int i = 0 ; i < jsonArray.length() ; i++){
                JSONObject statuse = jsonArray.getJSONObject(i) ;
                getInternetDataBean = new GetInternetDataBean() ;
                getInternetDataBean.setContent(statuse.getString("text"));
                getInternetDataBean.setWeiboId(statuse.getString("idstr"));
                getInternetDataBean.setUserId(userToken.getUserId());
                getInternetDataBean.setSendTime(statuse.getString("created_at"));
                getInternetDataBean.setContentPictureUrl(statuse.getString("pic_urls"));

                JSONObject user = jsonObject.getJSONObject("user") ;

                getInternetDataBean.setSenderId(user.getString("idstr"));
                getInternetDataBean.setSenderName(user.getString("name"));
                getInternetDataBean.setSenderIconUrl(user.getString("profile_image_url"));

                JSONObject origin = jsonObject.getJSONObject("retweeted_status") ;

                if( origin == null ){
                    getInternetDataBean.setIsRetransmit(0);
                }
                else {
                    getInternetDataBean.setIsRetransmit(1);
                    getInternetDataBean.setOriginalContent(origin.getString("text"));
                    getInternetDataBean.setOriginalPictureUrl(origin.getString("pic_urls"));

                    JSONObject originuser = origin.getJSONObject("user") ;

                    getInternetDataBean.setOriginalSenderId(originuser.getString("id")) ;
                    getInternetDataBean.setOriginalSenderName(originuser.getString("name"));

                }
            }






        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected String doInBackground(UserToken... params) {

        this.userToken = params[0] ;

        String request = WeiboConstant.FRIENDS_TIMELINE + WeiboConstant.ACCESS_TOKEN + params[0].getUserToken() + WeiboConstant.COUNT + 10 ;

        this.httpSupport = new httpSupport(request) ;

        return httpSupport.sendRequest();
    }
}
